package cn.iocoder.yudao.module.trade.cart.dal.mysql;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.module.trade.cart.dal.dataobject.CartDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Nullable;
import org.apache.ibatis.annotations.Mapper;

import java.util.*;

@Mapper
public interface CartMapper extends CoreMapper<CartDO> {

    default CartDO selectByUserIdAndSkuId(Long userId, Long skuId) {
        return selectOne(CartDO::getUserId, userId,
                CartDO::getSkuId, skuId);
    }

    default Integer selectSumByUserIdAndShopId(Long userId, @Nullable Long shopId) {
        QueryWrapper<CartDO> wrapper = new QueryWrapper<>();
        wrapper.select("SUM(count) AS sumCount");
        if (shopId != null && shopId > 0) {
            wrapper.eq("shop_id", shopId);
        }
        wrapper.eq("user_id", userId);
        List<Map<String, Object>> result = selectMaps(wrapper);
        return CollUtil.getFirst(result) != null ? MapUtil.getInt(result.get(0), "sumCount") : 0;
    }

    default Integer selectSumByUserIdAndShopIds(Long userId, Set<Long> shopIds) {
        if (CollUtil.isEmpty(shopIds)) {
            return 0;
        }
        QueryWrapper<CartDO> wrapper = new QueryWrapper<>();
        wrapper.select("SUM(count) AS sumCount");
        wrapper.eq("user_id", userId);
        wrapper.in("shop_id", shopIds);
        List<Map<String, Object>> result = selectMaps(wrapper);
        return CollUtil.getFirst(result) != null ? MapUtil.getInt(result.get(0), "sumCount") : 0;
    }

    default CartDO selectById(Long id, Long userId) {
        return selectOne(CartDO::getId, id,
                CartDO::getUserId, userId);
    }

    default List<CartDO> selectListByIds(Collection<Long> ids, Long userId) {
        return selectList(new LambdaQueryWrapper<CartDO>()
                .in(CartDO::getId, ids)
                .eq(CartDO::getUserId, userId)
                .orderByDesc(CartDO::getId));
    }

    default List<CartDO> selectListByUserIdAndShopId(Long userId, Long shopId) {
        return selectList(new CoreLambdaQueryWrapper<CartDO>()
                .eq(CartDO::getShopId, shopId)
                .eq(CartDO::getUserId, userId)
                .orderByDesc(CartDO::getId));
    }

    default List<CartDO> selectListByUserIdAndShopIds(Long userId, Set<Long> shopIds) {
        if (CollUtil.isEmpty(shopIds)) {
            return new ArrayList<>();
        }
        return selectList(new CoreLambdaQueryWrapper<CartDO>()
                .eq(CartDO::getUserId, userId)
                .in(CartDO::getShopId, shopIds)
                .orderByDesc(CartDO::getId));
    }

    default List<CartDO> selectListByUserIdAndIds(Long userId, Set<Long> ids) {
        return selectList(new LambdaQueryWrapper<CartDO>()
                .eq(CartDO::getUserId, userId)
                .in(CartDO::getId, ids)
                .orderByDesc(CartDO::getId));
    }

    default void updateByIds(Collection<Long> ids, Long userId, CartDO updateObj) {
        update(updateObj, new LambdaQueryWrapper<CartDO>()
                .in(CartDO::getId, ids)
                .eq(CartDO::getUserId, userId));
    }

}
